﻿#include "TucsonGeneral.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../SpellObject.h"
#include "../../../Shared/Enums.h"
#include "../DelayedAction.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char TucsonGeneral::getAttackRange() const
    {
        return 8;
    }

    TucsonGeneral::TucsonGeneral(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool TucsonGeneral::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    void TucsonGeneral::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 2);

        if (getEnvir()->getTime() > _RageTime)
        {
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 0;
            Broadcast(tempVar);

            _RageTime = getEnvir()->getTime() + 20000;

            AttackTime = getEnvir()->getTime() + 8000;

            auto targets = FindAllTargets(10, getCurrentLocation(), false);

            for (int i = 0; i < _RockCount; i++)
            {
                Point *location = new Point(getCurrentLocation()->X + getEnvir()->Random->Next(-Info->ViewRange, Info->ViewRange + 1), getCurrentLocation()->Y + getEnvir()->Random->Next(-Info->ViewRange, Info->ViewRange + 1));

                if (getEnvir()->Random->Next(3) == 0 && targets.size() > 0)
                {
                    location = targets[getEnvir()->Random->Next(targets.size())]->getCurrentLocation();
                }

                if (location->X == getCurrentLocation()->X || location->Y == getCurrentLocation()->Y)
                {
                    delete location;
                    continue;
                }

                auto start = getEnvir()->Random->Next(0, 5000);
                auto value = getEnvir()->Random->Next(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

                auto spellObj = new SpellObject();
                spellObj->Spell = Spell::TucsonGeneralRock;
                spellObj->Value = value;
                spellObj->ExpireTime = getEnvir()->getTime() + 2000 + start;
                spellObj->TickSpeed = 1000;
                spellObj->Caster = nullptr;
                spellObj->setCurrentLocation(location);
                spellObj->setCurrentMap(getCurrentMap());
                spellObj->setDirection(MirDirection::Up);
                spellObj->StartTime = getEnvir()->getTime() + 1000 + start;

                DelayedAction *action = new DelayedAction(DelayedType::Spawn, getEnvir()->getTime() + start, {spellObj});
                getCurrentMap()->ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete spellObj' statement was not added since spellObj was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete location' statement was not added since location was assigned to another object. Handle memory management manually.
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        if (!ranged && getEnvir()->Random->Next(4) > 0)
        {
            if (getEnvir()->Random->Next(3) > 0)
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 0;
                Broadcast(tempVar2);
                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility, false});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar3 = new S::ObjectAttack();
                tempVar3->ObjectID = ObjectID;
                tempVar3->Direction = getDirection();
                tempVar3->Location = getCurrentLocation();
                tempVar3->Type = 1;
                Broadcast(tempVar3);
                int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility, true});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            if (getEnvir()->Random->Next(4) > 0)
            {
                S::ObjectRangeAttack *tempVar4 = new S::ObjectRangeAttack();
                tempVar4->ObjectID = ObjectID;
                tempVar4->Direction = getDirection();
                tempVar4->Location = getCurrentLocation();
                tempVar4->TargetID = getTarget()->ObjectID;
                tempVar4->Type = 1;
                Broadcast(tempVar4);
                int damage = GetAttackPower(Stats[Stat::MinSC], Stats[Stat::MaxSC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                ProjectileAttack(Stats[Stat::MinSC], Stats[Stat::MaxSC], DefenceType::MACAgility);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectRangeAttack *tempVar5 = new S::ObjectRangeAttack();
                tempVar5->ObjectID = ObjectID;
                tempVar5->Direction = getDirection();
                tempVar5->Location = getCurrentLocation();
                tempVar5->TargetID = getTarget()->ObjectID;
                tempVar5->Type = 2;
                Broadcast(tempVar5);
                int damage = GetAttackPower(Stats[Stat::MinSC], Stats[Stat::MaxSC] * 2);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar5' statement was not added since tempVar5 was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility, true});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar5' statement was not added since tempVar5 was passed to a method or constructor. Handle memory management manually.
            }
        }
    }

    void TucsonGeneral::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        bool stomp = std::any_cast<bool>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (stomp)
        {
            auto targets = FindAllTargets(3, getCurrentLocation(), false);

            for (int i = 0; i < targets.size(); i++)
            {
                if (targets[i]->Attacked(this, damage, defence) <= 0)
                {
                    continue;
                }

                PoisonTarget(targets[i], 3, 5, PoisonType::Paralysis, 1000);
            }
        }
        else
        {
            target->Attacked(this, damage, defence);
        }
    }

    void TucsonGeneral::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
